﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace MetaSharp.Transformation
{
    public abstract class PatternTransformer : ITransformer
    {
        protected abstract IImmutableArray<Pattern> Steps { get; }

        public async Task<TransformResult> Transform(TransformationContext context)
        {
            var pattern = Pattern.Transform(Steps).ToPatternMatch();
            var stream = this.GetInput(context.Content);
            var scope = new Scope(stream);
            var match = await pattern(scope);
            var output = match.Matched
                ? this.GetOutput(context, match)
                : new TransformOutput[0];

            return new TransformResult(output, match.Errors);
        }

        protected abstract IStream GetInput(System.IO.Stream content);
        protected abstract IEnumerable<TransformOutput> GetOutput(TransformationContext context, IMatch match);
    }
}
